package com.bflow.util;

import org.dozer.DozerBeanMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @name Bean转换工具类
 * @author choudou5
 * @date 2018-7-13
 */
public class BeanMapper {

    private static DozerBeanMapper dozer = new DozerBeanMapper();

    public BeanMapper() {
    }

    public static <T> T map(Object source, Class<T> destinationClass) {
        return source != null && destinationClass != null?dozer.map(source, destinationClass):null;
    }

    public static void copy(Object source, Object destinationObject) {
        if(source != null && destinationObject != null) {
            dozer.map(source, destinationObject);
        }
    }

    public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass) {
        ArrayList destinationList = new ArrayList();
        if(sourceList == null) {
            return destinationList;
        } else {
            Iterator iter = sourceList.iterator();
            while(iter.hasNext()) {
                Object sourceObject = iter.next();
                Object destinationObject = dozer.map(sourceObject, destinationClass);
                destinationList.add(destinationObject);
            }
            return destinationList;
        }
    }

}
